from bs4 import BeautifulSoup
import requests

url = "https://news.ycombinator.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')

# 提取新闻标题和链接
news_items = []
for item in soup.select('tr.athing'):
    title = item.select_one('span.titleline a')
    if title:
        news_items.append({
            'title': title.text,
            'url': title['href'],
            'source': 'Hacker News'
        })

print(news_items[:5])  # 打印前5条新闻